﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web;
using Shuba.Business;
using Shuba.Model;

namespace Shuba.Web.mobile
{
    public partial class user_device_add : basePage
    {
        string msg = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtNIP.ClientID;
            this.Form.DefaultButton = btnSubmit.UniqueID;

            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("MSTR_USER_DEVICE");

                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("MSTR_USER_DEVICE");                

                if (Request.QueryString["a"] == "ed")
                {
                    txtNIP.Enabled = false;
                    BindData();
                }                
            }
        }

        private void BindData()
        {
            string nip = Request.QueryString["id"];

            UserDevice b = new UserDevice();
            UserDeviceInfo info = b.Get(nip);

            hdNIP.Value = nip;
            txtNIP.Text = nip;
            txtIMEI.Text = info.IMEI;            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                UserDevice d = new UserDevice();                

                string nip = txtNIP.Text;
                string imei = txtIMEI.Text;
                string actType = "INSERT";

                UserDeviceInfo info = d.Get(nip);

                /*
                if (d.GetByIMEI(imei) != null)                
                    throw new Exception("IMEI already exists!");*/

                if (Request.QueryString["a"] == "ed")
                {
                    nip = hdNIP.Value;
                    info = d.Get(nip);
                    if (info == null)
                        throw new Exception("User Device not found!");

                    actType = "UPDATE";
                }
                else
                {
                    if (info != null)
                        throw new Exception("NIP already exists!");

                    info = new UserDeviceInfo();
                    info.NIP = nip;
                }
                                                                    
                info.IMEI = txtIMEI.Text;
                info.EntryBy = HttpContext.Current.User.Identity.Name;
                info.EntryDate = ProcessingDate();
                info.UpdateBy = HttpContext.Current.User.Identity.Name;
                info.UpdateDate = ProcessingDate();
                info.ActType = actType;                                
                d.Save(info);

                AddLog(actType, actType + " User Device");
                msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                AlertAndRedirect(msg, "user_device_list.aspx");
            }
            catch (Exception ex)
            {
                ErrBox.Visible = true;
                ErrMsg.Text = ex.Message;
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("user_device_list.aspx");
        }
    }
}
